using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Tune;

public class FormInstrumentSave : Form
{
	private IList<InstrumentConfig> configs = new List<InstrumentConfig>();

	private IContainer components = null;

	private GroupBox groupBox1;

	private CheckBox chkHide;

	private TextBox txtDescript;

	private Button btnOK;

	private Button btnCancel;

	private DataGridView dgvConfig;

	private DataGridViewTextBoxColumn colDescription;

	private DataGridViewTextBoxColumn colTime;

	private DataGridViewTextBoxColumn colUser;

	private GroupBox groupBox2;

	private RadioButton radioButton1;

	private RadioButton rbtnStandard;

	private RadioButton rbtnDefine;

	public InstrumentConfig CurrentConfig { get; set; }

	public string Description { get; set; }

	public FormInstrumentSave()
	{
		InitializeComponent();
	}

	private void BindConfigs()
	{
		dgvConfig.AutoGenerateColumns = false;
		if (dgvConfig.DataSource == null)
		{
			dgvConfig.DataSource = configs;
			return;
		}
		BindingContext[dgvConfig.DataSource].SuspendBinding();
		dgvConfig.DataSource = null;
		dgvConfig.DataSource = configs;
		BindingContext[dgvConfig.DataSource].ResumeBinding();
	}

	private void LoadInstrumentsetings()
	{
		IList<InstrumentConfig> cgs = DataBase.Instance.GetAllInstrumentConfigs();
		if (cgs != null)
		{
			if (chkHide.Checked)
			{
				configs = cgs.Distinct(new InstrumentConfigCompare()).ToList();
			}
			else
			{
				configs = cgs;
			}
		}
		BindConfigs();
	}

	private void chkHide_CheckedChanged(object sender, EventArgs e)
	{
		LoadInstrumentsetings();
	}

	private void btnOK_Click(object sender, EventArgs e)
	{
		if (rbtnDefine.Checked)
		{
			if (txtDescript.Text == "")
			{
				MessageBox.Show(Lang.Txt("FormInstrumentSave.MessageBox.DescriptionName.Text"));
				return;
			}
			Description = txtDescript.Text;
		}
		else if (rbtnStandard.Checked)
		{
			Description = Lang.Txt("Tune.StandardMode");
		}
		else if (rbtnStandard.Checked)
		{
			Description = Lang.Txt("Tune.KEDMode");
		}
		foreach (InstrumentConfig config in configs)
		{
			if (config.Description == Description)
			{
				if (MessageBox.Show("该配置已存在/n,是否更新该配置文件", Lang.Txt("AddFormIgnite.Warn"), MessageBoxButtons.YesNo) != DialogResult.Yes)
				{
					base.DialogResult = DialogResult.Cancel;
					return;
				}
				DataBase.Instance.DeleteInstrumentConfig(config);
				base.DialogResult = DialogResult.OK;
			}
		}
		base.DialogResult = DialogResult.OK;
	}

	private void btnCancel_Click(object sender, EventArgs e)
	{
		base.DialogResult = DialogResult.Cancel;
	}

	private void FormInstrumentSave_Load(object sender, EventArgs e)
	{
		LoadInstrumentsetings();
		ApplyResources();
	}

	private void ApplyResources()
	{
		Text = Lang.Txt("FormInstrumentSave.Title.Text");
		colDescription.HeaderText = Lang.Txt("FormInstrumentSave.Columns.Description.Text");
		colTime.HeaderText = Lang.Txt("FormInstrumentSave.Columns.DateTime.Text");
		colUser.HeaderText = Lang.Txt("FormInstrumentSave.Columns.User.Text");
		groupBox1.Text = Lang.Txt("FormInstrumentSave.ConfigItems.Text");
		btnCancel.Text = Lang.Txt("FormInstrumentSave.Button.Cancel.Text");
		btnOK.Text = Lang.Txt("FormInstrumentSave.Button.OK.Text");
		chkHide.Text = Lang.Txt("FormInstrumentSave.HideSameConfig.Text");
	}

	private void dgvConfig_MouseClick(object sender, MouseEventArgs e)
	{
		if (e.Button != MouseButtons.Right || dgvConfig.SelectedCells.Count <= 0)
		{
			return;
		}
		List<int> index = new List<int>();
		foreach (DataGridViewCell item in dgvConfig.SelectedCells)
		{
			if (item.RowIndex >= 0)
			{
				index.Add(item.RowIndex);
			}
		}
		index = index.Distinct().ToList();
		ContextMenu menu = new ContextMenu();
		menu.MenuItems.Add(Lang.Txt("FormInstrumentOpen.Menu.Delete.Text"), delegate
		{
			DeleteSelectedConfigs(index);
		});
		menu.Show(dgvConfig, e.Location);
	}

	private void DeleteSelectedConfigs(List<int> index)
	{
		List<InstrumentConfig> cfgs = new List<InstrumentConfig>();
		foreach (int row in index)
		{
			cfgs.Add(configs[row]);
		}
		foreach (InstrumentConfig cfg in cfgs)
		{
			if (DataBase.Instance.DeleteInstrumentConfig(cfg))
			{
				configs.Remove(cfg);
				if (CurrentConfig == cfg)
				{
					CurrentConfig = null;
				}
			}
		}
		BindConfigs();
	}

	private void dgvConfig_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
	{
		if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
		{
			FormInstrumentConfig formInstrumentConfig = new FormInstrumentConfig(configs[e.RowIndex].Time);
			formInstrumentConfig.ShowDialog();
		}
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.dgvConfig = new System.Windows.Forms.DataGridView();
		this.colDescription = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colTime = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.colUser = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.chkHide = new System.Windows.Forms.CheckBox();
		this.txtDescript = new System.Windows.Forms.TextBox();
		this.btnOK = new System.Windows.Forms.Button();
		this.btnCancel = new System.Windows.Forms.Button();
		this.groupBox2 = new System.Windows.Forms.GroupBox();
		this.rbtnDefine = new System.Windows.Forms.RadioButton();
		this.rbtnStandard = new System.Windows.Forms.RadioButton();
		this.radioButton1 = new System.Windows.Forms.RadioButton();
		this.groupBox1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.dgvConfig).BeginInit();
		this.groupBox2.SuspendLayout();
		base.SuspendLayout();
		this.groupBox1.Controls.Add(this.dgvConfig);
		this.groupBox1.Controls.Add(this.chkHide);
		this.groupBox1.Location = new System.Drawing.Point(16, 15);
		this.groupBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Padding = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.groupBox1.Size = new System.Drawing.Size(929, 249);
		this.groupBox1.TabIndex = 0;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "当前仪器配置";
		this.dgvConfig.AllowUserToAddRows = false;
		this.dgvConfig.AllowUserToDeleteRows = false;
		this.dgvConfig.BackgroundColor = System.Drawing.Color.White;
		this.dgvConfig.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
		dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle3.BackColor = System.Drawing.SystemColors.Control;
		dataGridViewCellStyle3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle3.ForeColor = System.Drawing.SystemColors.WindowText;
		dataGridViewCellStyle3.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle3.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle3.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
		this.dgvConfig.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle3;
		this.dgvConfig.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dgvConfig.Columns.AddRange(this.colDescription, this.colTime, this.colUser);
		dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
		dataGridViewCellStyle4.BackColor = System.Drawing.SystemColors.Window;
		dataGridViewCellStyle4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
		dataGridViewCellStyle4.ForeColor = System.Drawing.SystemColors.ControlText;
		dataGridViewCellStyle4.SelectionBackColor = System.Drawing.SystemColors.Highlight;
		dataGridViewCellStyle4.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
		dataGridViewCellStyle4.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
		this.dgvConfig.DefaultCellStyle = dataGridViewCellStyle4;
		this.dgvConfig.GridColor = System.Drawing.SystemColors.Control;
		this.dgvConfig.Location = new System.Drawing.Point(9, 25);
		this.dgvConfig.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.dgvConfig.Name = "dgvConfig";
		this.dgvConfig.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
		this.dgvConfig.RowHeadersVisible = false;
		this.dgvConfig.RowTemplate.Height = 23;
		this.dgvConfig.Size = new System.Drawing.Size(912, 216);
		this.dgvConfig.TabIndex = 1;
		this.dgvConfig.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(dgvConfig_CellMouseDoubleClick);
		this.dgvConfig.MouseClick += new System.Windows.Forms.MouseEventHandler(dgvConfig_MouseClick);
		this.colDescription.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colDescription.DataPropertyName = "Description";
		this.colDescription.HeaderText = "说明";
		this.colDescription.Name = "colDescription";
		this.colDescription.ReadOnly = true;
		this.colTime.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.colTime.DataPropertyName = "Time";
		this.colTime.HeaderText = "时间";
		this.colTime.Name = "colTime";
		this.colTime.ReadOnly = true;
		this.colUser.DataPropertyName = "User";
		this.colUser.HeaderText = "用户";
		this.colUser.Name = "colUser";
		this.colUser.ReadOnly = true;
		this.chkHide.AutoSize = true;
		this.chkHide.Location = new System.Drawing.Point(495, 0);
		this.chkHide.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.chkHide.Name = "chkHide";
		this.chkHide.Size = new System.Drawing.Size(149, 19);
		this.chkHide.TabIndex = 0;
		this.chkHide.Text = "隐藏相同说明配置";
		this.chkHide.UseVisualStyleBackColor = true;
		this.chkHide.CheckedChanged += new System.EventHandler(chkHide_CheckedChanged);
		this.txtDescript.Location = new System.Drawing.Point(563, 19);
		this.txtDescript.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.txtDescript.Name = "txtDescript";
		this.txtDescript.Size = new System.Drawing.Size(281, 25);
		this.txtDescript.TabIndex = 2;
		this.txtDescript.Text = "Default";
		this.txtDescript.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.btnOK.Location = new System.Drawing.Point(336, 340);
		this.btnOK.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.btnOK.Name = "btnOK";
		this.btnOK.Size = new System.Drawing.Size(100, 29);
		this.btnOK.TabIndex = 3;
		this.btnOK.Text = "确定";
		this.btnOK.UseVisualStyleBackColor = true;
		this.btnOK.Click += new System.EventHandler(btnOK_Click);
		this.btnCancel.Location = new System.Drawing.Point(499, 340);
		this.btnCancel.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		this.btnCancel.Name = "btnCancel";
		this.btnCancel.Size = new System.Drawing.Size(100, 29);
		this.btnCancel.TabIndex = 4;
		this.btnCancel.Text = "取消";
		this.btnCancel.UseVisualStyleBackColor = true;
		this.btnCancel.Click += new System.EventHandler(btnCancel_Click);
		this.groupBox2.Controls.Add(this.radioButton1);
		this.groupBox2.Controls.Add(this.rbtnStandard);
		this.groupBox2.Controls.Add(this.rbtnDefine);
		this.groupBox2.Controls.Add(this.txtDescript);
		this.groupBox2.Location = new System.Drawing.Point(16, 268);
		this.groupBox2.Name = "groupBox2";
		this.groupBox2.Size = new System.Drawing.Size(929, 56);
		this.groupBox2.TabIndex = 5;
		this.groupBox2.TabStop = false;
		this.groupBox2.Text = "新配置说明";
		this.rbtnDefine.AutoSize = true;
		this.rbtnDefine.Location = new System.Drawing.Point(483, 23);
		this.rbtnDefine.Name = "rbtnDefine";
		this.rbtnDefine.Size = new System.Drawing.Size(73, 19);
		this.rbtnDefine.TabIndex = 3;
		this.rbtnDefine.Text = "自定义";
		this.rbtnDefine.UseVisualStyleBackColor = true;
		this.rbtnStandard.AutoSize = true;
		this.rbtnStandard.Checked = true;
		this.rbtnStandard.Location = new System.Drawing.Point(230, 25);
		this.rbtnStandard.Name = "rbtnStandard";
		this.rbtnStandard.Size = new System.Drawing.Size(88, 19);
		this.rbtnStandard.TabIndex = 3;
		this.rbtnStandard.TabStop = true;
		this.rbtnStandard.Text = "标准模式";
		this.rbtnStandard.UseVisualStyleBackColor = true;
		this.radioButton1.AutoSize = true;
		this.radioButton1.Location = new System.Drawing.Point(360, 24);
		this.radioButton1.Name = "radioButton1";
		this.radioButton1.Size = new System.Drawing.Size(82, 19);
		this.radioButton1.TabIndex = 3;
		this.radioButton1.Text = "KED模式";
		this.radioButton1.UseVisualStyleBackColor = true;
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(958, 382);
		base.Controls.Add(this.groupBox2);
		base.Controls.Add(this.btnCancel);
		base.Controls.Add(this.btnOK);
		base.Controls.Add(this.groupBox1);
		base.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
		base.MaximizeBox = false;
		base.MinimizeBox = false;
		base.Name = "FormInstrumentSave";
		base.ShowIcon = false;
		this.Text = "保存仪器配置";
		base.Load += new System.EventHandler(FormInstrumentSave_Load);
		this.groupBox1.ResumeLayout(false);
		this.groupBox1.PerformLayout();
		((System.ComponentModel.ISupportInitialize)this.dgvConfig).EndInit();
		this.groupBox2.ResumeLayout(false);
		this.groupBox2.PerformLayout();
		base.ResumeLayout(false);
	}
}
